const http = require('http');
const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'customize'
});

const hostname = '127.0.0.1';
const port = 3000;


const server = http.createServer((req, res) => {
    // 设置状态码
    res.statusCode = 200;
    // 设置请求头
    res.setHeader('Content-Type', 'text/plain; charset=utf-8');
    // 解决跨域问题
    res.setHeader("Access-Control-Allow-Origin", "*"); 
    console.log(req.url);
    if(req.url == '/favicon.ico'){
        return;
    }
    const createTableQuery = 'select * from user'; // 查询表
    // const createTableQuery = `'select * from user where name like '${'张三'}'`; // 在表中查询
    connection.query(createTableQuery, (error, results, fields) => {
        if (error) throw error;
        console.log(JSON.stringify(results));
    });
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
}); 




